package com.kfm.wiki.filter;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

//@Component
public class LogFilter implements Filter {
    private static final Logger LOG = LoggerFactory.getLogger(LogFilter.class);

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
         LOG.info("------------- LogFilter 开始 -------------");
         LOG.info("请求地址: {} {}", httpServletRequest.getRequestURI().toString(), httpServletRequest.getMethod());
         LOG.info("远程地址: {}", httpServletRequest.getRemoteAddr());

         long startTime = System.currentTimeMillis();
         chain.doFilter(request, response);
         LOG.info("------------- LogFilter 结束 耗时：{} ms -------------", System.currentTimeMillis() - startTime);
    }
}
